var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function () {
        return {
            isOpen: true,
            selecttypeid:'',
        }
    },
    computed: {
        isFolder: function () {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function (item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function (item) {
            var that = this;
            app.currentchosenlist.id = item.ID;
            that.selecttypeid=item.ID;
            app.currentchosenlist.text = item.text;
            setTimeout(function () {
                app.is_type_list = false;
            }, 100)
        },
    }
})
app = new Vue({
    
    el: '#app',
    data: {
        ProjectList: [],
        CompanyID: 0,
        FullName: '',
        isshow: false,
        list: [],
        childlist: [],
        currentchosenlist: [],
        form: {
            keywords: ''
        },
        // type:'',
        placetype:0,//1-微信公众号来的工单类型
        gongdanType:1,//1-报事报修，2-投诉建议
        ShenHeType:1,//1-通用审批,2-用印审批
    },

    methods: {
        get_message:function(){
            var that = this;
            ns.post({
                keywords: that.form.keywords,
                Type:that.gongdanType,
                action: 'getservicetypelist',
            }, function(succeed, data, err) {
                if (succeed) {
                    if (data.data.length <= 0) {
                        api.toast({
                            msg: '暂无相关数据',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    //that.list = data.list;
                    that.currentchosenlist = data.data;
                    for (var i = 0; i < that.currentchosenlist.length; i++) {
                        var item = that.currentchosenlist[i];
                        // if (item.TypeID == 2) {
                        //     that.ProjectList.push({
                        //         id: item.id,
                        //         text: item.text,
                        //         TypeID: item.TypeID
                        //     });
                            for (var j = 0; j < item.children.length; j++) {
                                item.children[j]['ParentID'] = item.id;
                                that.childlist.push(item.children[j]);
                            }
                        // }
                    }
                    that.FullName = that.currentchosenlist[0].text;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        onPriceSearch:function(){
            var that=this;
            if(that.form.keywords==''){
                that.get_data();
            }
        },
        get_type:function(){
            var that = this;
            ns.post({
                keywords: that.form.keywords,
                action: 'loadtsealbasic',
            }, function(succeed, data, err) {
                if (succeed) {
                    if(that.ShenHeType==1){
                        if (data.tongyong_list.length <= 0) {
                            api.toast({
                                msg: '暂无相关数据',
                                duration: 2000,
                                location: 'bottom'
                            });
                            return;
                        }
                        that.currentchosenlist = data.tongyong_list;
                    }else{
                        if (data.yongying_list.length <= 0) {
                            api.toast({
                                msg: '暂无相关数据',
                                duration: 2000,
                                location: 'bottom'
                            });
                            return;
                        }
                        that.currentchosenlist = data.yongying_list;
                    }
                    
                    for (var i = 0; i < that.currentchosenlist.length; i++) {
                        var item = that.currentchosenlist[i];
                        // if (item.TypeID == 2) {
                        //     that.ProjectList.push({
                        //         id: item.id,
                        //         text: item.text,
                        //         TypeID: item.TypeID
                        //     });
                            for (var j = 0; j < item.children.length; j++) {
                                item.children[j]['ParentID'] = item.id;
                                that.childlist.push(item.children[j]);
                            }
                        // }
                    }
                    that.FullName = that.currentchosenlist[0].text;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });

        },

        get_data: function() {
            var that = this;
            if(that.ShenHeType==1||that.ShenHeType==2){
                that.get_type();
            }else{
                that.get_message();
            }
            
        },
        do_save:function(item){
            var that=this;
            var options={};
            // options.action = "dopublicservice";
            options.id=that.currentchosenlist.id;
            options.ID=that.currentchosenlist.ID;
            options.text=that.currentchosenlist.text;
            options.TypeID=that.currentchosenlist.TypeID;
            options.children=that.currentchosenlist.children;
            if(that.ShenHeType==1){
                api.sendEvent({
                    name: 'dosaveshenhetype',
                    extra: {
                        item: options,
                    }
                });
            }else if(that.ShenHeType==2){
                api.sendEvent({
                    name: 'dosaveshenheyongyintype',
                    extra: {
                        item: options,
                    }
                });
            }else{
                api.sendEvent({
                    name: 'dosaveservicetype',
                    extra: {
                        item: options,
                    }
                });
            }
            
            setTimeout(function() {
                api.closeWin();
            }, 500);

        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        getChildListByParentID: function(ParentID) {
            var that = this;
            that.currentchosenlist = [];
            for (var i = 0; i < that.childlist.length; i++) {
                if (that.childlist[i].ParentID == ParentID) {
                    that.currentchosenlist.push(that.childlist[i]);
                }
            }
        },
        do_select_room: function() {
            var that = this;
            var list = that.ProjectList;
            if (list.length == 0) {
                return;
            }
            var UIMultiSelector = api.require('UIMultiSelector');
            UIMultiSelector.open({
                rect: {
                    h: 244
                },
                text: {
                    title: '选择公司',
                    leftBtn: '取消',
                    rightBtn: '完成',
                    selectAll: ''
                },
                max: 0,
                singleSelection: true,
                styles: {
                    bg: '#fff',
                    mask: 'transparent',
                    title: {
                        bg: '#ddd',
                        color: '#808080',
                        size: 16,
                        h: 44
                    },
                    leftButton: {
                        bg: '#03A9F2',
                        w: 80,
                        h: 35,
                        marginT: 5,
                        marginL: 8,
                        color: '#fff',
                        size: 14
                    },
                    rightButton: {
                        bg: '#03A9F2',
                        w: 80,
                        h: 35,
                        marginT: 5,
                        marginR: 8,
                        color: '#fff',
                        size: 14
                    },
                    item: {
                        h: 35,
                        bg: '#fff',
                        bgActive: '#4B9CE3',
                        bgHighlight: '#4B9CE3',
                        color: '#808080',
                        active: '#fff',
                        highlight: '#fff',
                        size: 14,
                        lineColor: '#ddd',
                        textAlign: 'center'
                    },
                    icon: {
                        w: 0,
                        marginH: 0
                    }
                },
                animation: true,
                items: list
            }, function(ret, err) {
                if (ret) {
                    if (ret.eventType == 'clickRight') {
                        if (ret.items.length > 0) {
                            that.FullName = ret.items[0].text;
                        }
                        that.getChildListByParentID(ret.items[0].id);
                        UIMultiSelector.close();
                    }
                    if (ret.eventType == 'clickLeft' || ret.eventType == 'clickMask') {
                        UIMultiSelector.close();
                    }
                }
            });
        },
        // show_morehistory: function(item) {
        //     var that = this;
        //     item.isopen = !item.isopen;
        // },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_search: function() {
            var that = this;
            that.get_data();
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.gongdanType=api.pageParam.gongdanType;//1-报事报修，2-投诉建议
    app.placetype=api.pageParam.placetype;
    app.ShenHeType=api.pageParam.ShenHeType;//1-通用审批，2-用印审批
    app.get_data();
    
};
